package com.free.semantic.core.proxy;

import com.free.semantic.common.RelationItem;
import com.free.semantic.core.ConceptInstanceManage;
import com.free.semantic.core.ConceptManage;
import com.free.semantic.core.FunctionProvider;
import com.free.semantic.core.RelationFunction;
import com.free.semantic.utils.Utils;

import java.util.HashMap;
import java.util.Map;

public class ProxyFunctionProvider implements FunctionProvider {

    private final Map<RelationItem, RelationFunction> functionMap = new HashMap<>();

    private ConceptManage mainManage;

    private ConceptInstanceManage providerManage;

    public ProxyFunctionProvider(ConceptManage mainManage, ConceptInstanceManage providerManage) {
        this.mainManage = mainManage;
        this.providerManage = providerManage;
    }

    public void prepareFunctionMap() {
        Map<RelationItem, RelationFunction> tempMap = new HashMap<>();
        Utils.forEachRelationItem(providerManage.concept(), relationItem -> {
            Utils.findSingleItem(mainManage, relationItem.getSourceObjList(), relationItem.getDestObj());
            RelationFunction function = providerManage.getFunction(relationItem.getSourceObjectList(), relationItem.getDestObject());
            if (function != null) {
                tempMap.put(relationItem, function);
            }
        });
        if (tempMap.isEmpty()) {
            throw new RuntimeException("注册器为空");
        }
        functionMap.putAll(tempMap);
    }

    public Map<RelationItem, RelationFunction> getFunctionMap() {
        return functionMap;
    }

    @Override
    public RelationFunction getFunction(RelationItem relationItem) {
        return functionMap.get(relationItem);
    }
}
